package top.yiciyuan.web.servlet;

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ImageIcon;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import cn.itcast.commons.CommonUtils;

public class UpLoadData extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//
		DiskFileItemFactory factory= new DiskFileItemFactory();
		
		ServletFileUpload sfu=new ServletFileUpload(factory);
		sfu.setFileSizeMax(100*1024*100);
		try {
			List<FileItem> fileItemList=sfu.parseRequest(request);
			
			Map<String, String> map=new HashMap<String, String>();
			
			
			for (FileItem fileItem : fileItemList) {
				if(fileItem.isFormField())
					map.put(fileItem.getFieldName(), fileItem.getString("utf-8"));
			}

			
//			System.out.println(form);
			
			//2.保存上传的文件
			//保存目录
			String savepath=this.getServletContext().getRealPath("/excel");
			
			//上传文件名称 前缀+文件名
			String filename = "dataexcel.xls";
//			System.out.println(filename);

			//创建目标文件
			File file=new File(savepath,filename);

			//保存上传文件到目标文件位置
			fileItemList.get(0).write(file);
			
			
			////////////////////////////////////////////////////////////////
				
			
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof FileUploadBase.FileSizeLimitExceededException)
			{
			}
			
		}
		request.getRequestDispatcher("/data.html").forward(request, response);
		
	}
}
